/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 4000;
ll t, n, m;
ll cnt;
string s[110];
struct node
{
    ll next_s[27];
    bool flag;
    // node(){
    //     memset(next_s,-1,sizeof(next_s));
    //     flag=false;
    // }
} a[maxn];
ll ans;
void insert1(ll id, ll len)
{
    ll p = 0, c;
    for (ll i = 0; i < len; i++)
    {
        if (s[id][i] == '/')
        {
            c = 26;
        }
        else
            c = s[id][i] - 'a';
        if (a[p].next_s[c] == -1)
        {
            a[p].next_s[c] = ++cnt;
            memset(a[cnt].next_s, -1, sizeof(a[cnt].next_s));
            a[cnt].flag = false;
        }
        p = a[p].next_s[c];
    }
    return;
}

void find(ll id, ll len)
{
    ll p = 0, c;
    for (ll i = 0; i < len; i++)
    {
        if (s[id][i] == '/')
        {
            c = 26;
        }
        else
            c = s[id][i] - 'a';
        if (a[p].next_s[c] == -1)
        {
            a[p].next_s[c] = ++cnt;
            memset(a[cnt].next_s, -1, sizeof(a[cnt].next_s));
            if (c == 26)
            {
                a[cnt].flag = true;
                ans++;
                // cout << id << endl;
                return;
            }
            else
            {
                a[cnt].flag = false;
            }
        }
        p = a[p].next_s[c];
        if (a[p].flag)
        {
            // ans++;
            // cout << "hehe" << endl;
            return;
        }
    }
    return;
}

int main()
{
    IOS;
    cin >> t;
    while (t--)
    {
        cnt = 0;
        ans = 0;
        memset(a[0].next_s, -1, sizeof(a[0].next_s));
        a[0].flag = false;
        cin >> n >> m;
        for (ll i = 1; i <= n; i++)
        {
            cin >> s[i];
            s[i].push_back('/');
        }
        for (ll i = n + 1; i <= n + m; i++)
        {
            cin >> s[i];
            s[i].push_back('/');
        }
        for (ll i = n + 1; i <= n + m; i++)
        {
            insert1(i, s[i].length());
        }
        for (ll i = 1; i <= n; i++)
        {
            find(i, s[i].length());
        }
        cout << ans << endl;
    }

    getchar();
    getchar();
    return 0;
}
